package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVo;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 业务:文件上传案例
     * @param file=[1010101010111]
     * @return
     * @throws IOException
     * 知识回顾: 字节流/字符流/缓存流
     * 高级API: SpringMVC 专门针对与流,开发了一个高级API
     * 文件上传步骤:
     *
     */
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file)throws IOException{

        ImageVo imageVo=fileService.upload(file);
        //不成功 返回空null
        if (imageVo == null)
        {
            return  SysResult.fail();
        }
        return SysResult.success(imageVo);
    }


    /**
     *实现文件的删除操作
     */
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath)
    {
          fileService.deleteFile(virtualPath);
          return  SysResult.success();
    }

}
/**文件上传入门案例
 *          //1.动态获取文件名称
 *         String fileName=file.getOriginalFilename();
 *         //2.准备文件目录   Linux 不识别'\'
 *         String dirPath="E:/CGBIII/CGBIIItools/vue/picture/jtadmin";
 *         File dirFile=new File(dirPath);
 *         if(!dirFile.exists())
 *         {
 *             dirFile.mkdirs();
 *         }
 *         //3.拼接文件路径---获取文件全限定路径
 *         String filePath = "E:/CGBIII/CGBIIItools/vue/picture/jtadmin/" + fileName;
 *         //4.实现文件上传
 *         file.transferTo(new File(filePath));
 *         System.out.println("实现文件上传");
 *         return  SysResult.success();
 */
